﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GatherWebLib.Model;
using System.Data;
using System.Text.RegularExpressions;

namespace GatherWebLib
{
   public class cGatherTask
    {
    
       cGatherTaskSplit dtc;

       /// <summary>
       /// 采集日志事件
       /// </summary>
       private event EventHandler<cGatherTaskLogArgs> e_Log;
       public event EventHandler<cGatherTaskLogArgs> Log
       {
           add { e_Log += value; }
           remove { e_Log -= value; }
       }

       public cGatherTask(cTask_m m_TaskData)
       {
           dtc = new cGatherTaskSplit();
           dtc.WebCode = (cGlobalParas.WebCode)int.Parse(m_TaskData.WebCode);
           dtc.TaskName = m_TaskData.TaskName;
           dtc.IsUrlEncode = m_TaskData.IsUrlEncode;
           dtc.UrlEncode = m_TaskData.UrlEncode;
           dtc.Cookie = m_TaskData.Cookie;
           dtc.StartPos = m_TaskData.StartPos;
           dtc.EndPos = m_TaskData.EndPos;
           dtc.SavePath = m_TaskData.SavePath;
           dtc.AgainNumber = m_TaskData.GatherAgainNumber;
           dtc.Ignore404 = m_TaskData.IsIgnore404;
           dtc.IsErrorLog = m_TaskData.IsErrorLog;
           dtc.Weblink = new List<cWebLink_m>();

           cWebLink_m w;
            cUrlAnalyze u = new cUrlAnalyze();

           foreach(cWebLink_m l in m_TaskData.WebpageLink)
           {
               w = new cWebLink_m();
               if (Regex.IsMatch(l.Weblink.ToString(), "{.*}"))
                {
                    List<string> Urls;

                    if (m_TaskData.IsUrlEncode == true)
                    {
                        Urls = u.SplitWebUrl(l.Weblink.ToString());
                    }
                    else
                    {
                        Urls = u.SplitWebUrl(l.Weblink.ToString());
                    }
                    
                    //开始添加m_TaskData.weblink数据
                    for (int j=0;j<Urls.Count ;j++)
                    {
                        w = new cWebLink_m();
                        w.IsGathered = l.IsGathered;
                        w.IsNavigation = l.IsNavigation;
                        w.IsNextpage = l.IsNextpage;
                        w.NextPageRule = l.NextPageRule;
                        w.Weblink = Urls[j].ToString();

                        //加载导航数据
                        if (l.IsNavigation == true)
                        {
                            w.NavigRules = l.NavigRules;
                        }

                        dtc.Weblink.Add(w);
                        w = null;
                    }

                }
                else
                {
                    dtc.Weblink.Add(l);
                }
           }

           

           dtc.CutFlag = m_TaskData.WebpageCutFlag;
           dtc.Log += this.onLog;

       }
       ~cGatherTask()
       {
           dtc = null;
       }
       public DataTable Start()
       {
           dtc.GatherUrl();
           DataTable dt = dtc.GatherData;
           return dt;
       }
       //处理日志事件
       private void onLog(object sender, cGatherTaskLogArgs e)
       {
           e_Log(sender, e);
       }
    }
}
